package com.lixun.passwordbook;

import com.lixun.passwordbook.pojo.Colors;
import com.lixun.passwordbook.util.FileData;
import com.lixun.passwordbook.slice.MainAbilitySlice;
import com.lixun.passwordbook.util.Jump;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.app.Context;
import ohos.global.configuration.Configuration;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbility extends Ability {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MainAbilityStart");

    private static int PAUSE_TIME = 5000;
    private static long startTime;

    /**
     * 声明静态变量，用于获取生物识别对象
     */
    private static MainAbility myAbility;

    /**
     * 私有构造
     */
    public MainAbility() {
        myAbility = this;
    }

    /**
     * 获取ability
     *
     * @return MainAbility
     */
    public static MainAbility getMainAbility() {
        return myAbility;
    }

    @Override
    public void onStart(Intent intent) {
        HiLog.info(LABEL,"onStart");
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
    }

    @Override
    protected void onActive() {
        super.onActive();
//        int i =  Configuration.DARK_MODE;
        Configuration configuration = new Configuration();
        Context context = getApplicationContext();
        FileData fileData = new FileData();
        // 若系统当前处于黑暗模式则设置auto_dark_mode为true
        if (configuration.getSystemColorMode() == 0){
            fileData.setBoolean(context,"dark_mode",true);
            Colors.setDarkMode(true);
        } else {
            fileData.setBoolean(context,"dark_mode",false);
            Colors.setDarkMode(false);
        }
        HiLog.info(LABEL,"onActive");
        HiLog.info(LABEL,"Configuration:" + configuration.getSystemColorMode());
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
//        Jump.setEndTime();
//        HiLog.info(LABEL,"onForeground");
//        HiLog.info(LABEL, "endtime:"+ Jump.getStartTime());
//        HiLog.info(LABEL, "endtime:"+ Jump.getEndTime());
//        HiLog.info(LABEL, "time:"+ Jump.getTime());
//        if (Jump.ifTrue()) {
//            toHome();
//        }
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        HiLog.info(LABEL,"onBackground");
//        Jump.setStartTime(System.currentTimeMillis());
//        HiLog.info(LABEL, "starttime:"+Jump.getStartTime());
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        HiLog.info(LABEL, "onInactive");
    }

    @Override
    protected void onStop() {
        super.onStop();
        HiLog.info(LABEL, "onStop");
    }

    //跳转到Home界面
    private void toHome() {
        Intent secondIntent = new Intent();
        // 指定待启动FA的bundleName和abilityName
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(HomeAbility.class.getName())
                .build();
        secondIntent.setOperation(operation);
        // 通过AbilitySlice的startAbility接口实现启动另一个页面
        startAbility(secondIntent);
        // 进入暂停状态通过onForeground可再次启动
//        terminate();
        terminateAbility();
    }
}

